/*
 * @Author: xiaosihan 
 * @Date: 2023-02-06 15:22:57 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-06-21 12:49:38
 */
import { AmbientLight, DirectionalLight, PointLight, Vector3 } from "three";
import ThreeBase from "three-base";

import { autorun } from "mobx";
import Sky from "three-base/Sky/Sky";
import BlastMesh from "./component3d/BlastMesh/BlastMesh";
import homeStore from "./homeStore";
import threeUtils from "three-base/threeUtils";

/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {

    constructor() {
        super();
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight("#fff", 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight("#fff", 1.5);
        pointLight.position.set(50, 40, 50);
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight("#fff", 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();

        this.resetLookAt({
            center: new Vector3(0, 0, 0),
            position: new Vector3(0, 3, 10)
        });

    }

    //天空
    sky = (() => {
        const sky = new Sky();
        this.scene.add(sky);
        return sky;
    })();

    //爆炸模型
    blastMesh = (() => {
        const blastMesh = new BlastMesh();
        this.scene.add(blastMesh);
        return blastMesh;
    })();

    //停止渲染就销毁监听
    stopRender(): void {
        this.dispose1();
        this.dispose2();
    }

    //监听爆炸进度公共状态 来设置爆炸模型的进度
    dispose1 = autorun(() => {
        const { blastProgress } = homeStore;
        this.blastMesh.setBlastProgress(blastProgress);
    });

    //监听爆炸模型的改变来加载爆炸模型
    dispose2 = autorun(() => {
        const { currentModal } = homeStore;
        this.blastMesh.loadModal(currentModal);
    });

    //监听选中的模型id 来设置需要高亮的模型
    dispose3 = autorun(() => {
        const { activeId, hideNotActive } = homeStore;
        this.blastMesh.setStroke(activeId);

        const { position } = this.getLookAt();
        if (activeId) {
            const activeMesh = this.blastMesh.getObjectByProperty("uuid", activeId);
            if (activeMesh) {
                const center = threeUtils.getCenter(activeMesh);
                //设置视角的中心
                this.setLookAt({ position, center });
            } else {
                this.setLookAt({ position, center: new Vector3() });
            }
        } else {
            this.setLookAt({ position, center: new Vector3() });
        }

        //隐藏其他的逻辑
        if (activeId) {
            this.blastMesh.onlyShow(hideNotActive, activeId);
        } else {
            this.blastMesh.onlyShow(false);
        }

    });

}


const homeRenderer = window.homeRenderer = new HomeRender();

export default homeRenderer;